package com.ryenlii.datatrans.core.kafka;

import com.alibaba.fastjson.JSON;
import com.ryenlii.datatrans.core.constant.TopicConst;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * 描述:
 *
 * @author zyzboole
 * @date 2023/07/11
 */
@Service
@Slf4j
public class KafkaService {

    @Value(TopicConst.PLATFORM_SEND_CITY_DATA)
    private String platformSendCityData;

    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    /**
     * 功能描述: 往外发消息
     *
     * @param topic
     * @param sendObj
     * @author xuhao
     * @date 2022-03-16 22:30:24
     */
    public void sendMessage(String topic, Object sendObj) {
        if (sendObj instanceof String) {
            kafkaTemplate.send(topic, sendObj);
        } else {
            kafkaTemplate.send(topic, JSON.toJSONString(sendObj));
        }
    }

    public void platformSendCityData(Object object) {
        sendMessage(platformSendCityData, object);
    }

}
